<template>
    <div>
        <div class="container">
            <div class="handle-box">
                <el-input v-model="query.keyword" placeholder="设备名称" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="func_handleSearch" plain>搜索</el-button>
                <el-button type="success" class="mr10" icon="el-icon-search" @click="func_handleAdd" plain>添加</el-button>
				<el-dropdown :hide-on-click="false">
                    <el-button class="mr10" plain>操作</el-button>
					<template #dropdown>
						<el-dropdown-menu>
							<el-dropdown-item type="text" icon="el-icon-lx-roundcheck" @click="func_handleBatchEditStatus(2001)" class="green"> 批量启用</el-dropdown-item>
                            <el-dropdown-item type="text" icon="el-icon-lx-roundclose" @click="func_handleBatchEditStatus(2002)" class="red"> 批量停用</el-dropdown-item>
						</el-dropdown-menu>
					</template>
				</el-dropdown>
            </div>
            <el-table :data="couponTableData" border class="table" header-cell-class-name="table-header" @selection-change="func_handleSelectionChange">
                <el-table-column type="selection" width="55" />
                <el-table-column prop="id" label="ID" width="55" align="center" v-if="false" />
				<el-table-column label="序号" width="80" align="center"><template #default="scope">
						{{scope.$index + 1}}
				</template></el-table-column>
                <el-table-column prop="couponName" label="券名称" />
				<el-table-column prop="strCouponAmount" label="优惠金额" />
                <el-table-column prop="strCouponType" label="券分类" />
                <el-table-column prop="strUseType" label="使用类型" />
                <el-table-column prop="specialType" label="特定类型" v-if="false"/>
                <el-table-column prop="lengthDays" label="有效时间(天)" />
                <el-table-column prop="strStartTime" label="开始时间" />
                <el-table-column prop="strExpTime" label="到期时间" />
				<el-table-column prop="" label="状态" >
                    <template #default="scope">
                        <el-tag :type=" scope.row.status === 2001 ? 'success' : scope.row.status === 2002 ? 'danger' : '' "> {{scope.row.strStatus}} </el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template #default="scope">
                        <el-button class="mr10" type="text" icon="el-icon-edit" @click="func_handleEdit(scope.$index, scope.row)">编辑</el-button>
                        <el-dropdown :hide-on-click="false">
                            <span class="el-dropdown-link green mr10"><el-icon class="el-icon-lx-info mr10" />更多</span>
                            <template #dropdown>
                                <el-dropdown-menu>
                            		<el-dropdown-item type="text" icon="el-icon-lx-roundcheck" @click="func_changeRowStatus(scope.$index, scope.row, 2001)" class="green"> 启用 </el-dropdown-item>
                            		<el-dropdown-item type="text" icon="el-icon-lx-roundclose" @click="func_changeRowStatus(scope.$index, scope.row, 2002)" class="red"> 停用 </el-dropdown-item>
                                </el-dropdown-menu>
                            </template>
                        </el-dropdown>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination background layout="total, prev, pager, next" :current-page="query.pageNum"
                    :page-size="query.pageSize" :total="dataTotal" @current-change="handlePageChange"></el-pagination>
            </div>
        </div>
        <!-- 编辑弹出框 -->
        <el-dialog title="编辑优惠券" v-model="vCouponDialogVisible" width="55%" :close-on-click-modal="false">
            <VCoupon v-bind:selectValue="vCouponData" @CouponData="func_loadvCouponData" />
        </el-dialog>
        <!-- ---------------------------------------------------------------------------------------------------------------------- -->
    </div>
</template>

<script>
import { ref, reactive, toRefs } from "vue";
import { useRouter, useRoute } from "vue-router";
import { ElMessage, ElMessageBox } from "element-plus";
import { findCouponListPage, batchEditCouponStatus } from "../../../api/hairmanager/coupon";
import { NO_DATA_SELECT_ERROR, SUCCESS_ADDDATA_DIALOG_MESSAGE, SUCCESS_DELDATA_DIALOG_MESSAGE, SUCCESS_EDITDATA_DIALOG_MESSAGE, formLabelWidth, } from "../../../api/CommonErrorMessage.js";
import VCoupon from "./components/couponForm.vue";

export default {
    name: "couponTable",
    components: {VCoupon },
    setup() {
        const router = useRouter();
		const route = useRoute();
        const optColumnWidth = 150;
        const query = reactive({
            keyword: "",
            isStatus: 2001,
            pageNum: 1,
            pageSize: 10,
        });
        const couponTableData = ref([]);
        const dataTotal = ref(0);
        const multipleSelection = ref([]);
        const vCouponData = reactive({
            load: false,
            id: 0,
        });
        let idx = -1;
        // =========================================================================================================
        // 主业务处理 ========================================================================================= START
        /**
         * @name 获取表格数据
         */
        const func_getData = () => {
            findCouponListPage(query).then((res) => {
                couponTableData.value = res.data.list;
                dataTotal.value = res.data.total;
            });
        };
        func_getData();
        /**
         * @name 主表格查询操作
         */
        const func_handleSearch = () => {
            query.pageNum = 1;
            func_getData();
        };
        /**
         * @name 分页导航
         */
        const handlePageChange = (val) => {
            query.pageNum = val;
            func_getData();
        };
        /**
         * @name 删除操作
         */
        const func_changeRowStatus = (index, row, isStatus) => {
			if(isStatus === 2002) {
				// 二次确认删除
				ElMessageBox.confirm("确定要停用数据吗？", "提示", {
				type: "warning",
				}).then(() => {
					let obj = {ids: [row['id']], status: isStatus};
					func_editCouponStatus(obj);
                    func_getData();
				}).catch(() => {});
			} else {
				let obj = {ids: [row['id']], status: isStatus};
				func_editCouponStatus(obj);
                func_getData();
			}
        };
		const func_editCouponStatus = (obj) => {
			batchEditCouponStatus(obj).then((res) => {
				if(res.code != 200) {
					if(res.type === null){
						ElMessage.error(res.message);
					}else if(res.type === 0) {
						ElMessage.warning(res.message);
					} else {
						ElMessage.error(res.message);
					}
				} else {
					ElMessage.success(SUCCESS_EDITDATA_DIALOG_MESSAGE);
					func_getData();
				}
			});
		}
        /**
         * @name 添加数据
         */
        const func_handleAdd = () => {
            vCouponData['id'] = 0;
            vCouponData['load'] = false;
            vCouponDialogVisible.value = true;
        };
        /**
         * @name 行编辑操作
         */
        const func_handleEdit = (index, row) => {
            idx = index;
            vCouponData['id'] = row['id'];
            vCouponData['load'] = true;
            vCouponDialogVisible.value = true;
        };
        const func_handleSelectionChange = (val) => {
            let ids = [];
            val.forEach((element) => {
                ids.push(element['id']);
            });
            multipleSelection.value = ids;
        };
        /**
         * @name 批次删除数据
         */
        const func_handleBatchEditStatus = (isStatus) => {
            if(multipleSelection.value.length ===0){
                ElMessage.warning(NO_DATA_SELECT_ERROR);
                return false;
            }
			if(isStatus === 2002) {
				// 二次确认删除
				ElMessageBox.confirm("确定要停用用户吗？", "提示", {
				type: "warning",
				}).then(() => {
					let obj = {ids: multipleSelection.value, isStatus: isStatus};
					func_editCouponStatus(obj);
				});
			} else {
				let obj = {ids: multipleSelection.value, isStatus: isStatus};
				func_editCouponStatus(obj);
			}
        }
        // 主业务处理 ========================================================================================= END
		// 扩展业务处理 ========================================================================================= START
		// 表格编辑时弹窗和保存
        const vCouponDialogVisible = ref(false);
        const func_loadvCouponData = (v) => {
            if(v['state'] === 1){
                vCouponDialogVisible.value = false;
                vCouponData['load'] = null;
                func_getData();
            } else {
                vCouponDialogVisible.value = true;
            }
        };
		// 扩展业务处理 ========================================================================================= END
        // 扩展业务处理 ========================================================================================= START
        // 扩展业务处理 ========================================================================================= END
        // =========================================================================================================
        return {
            // 系统变量 =========================================================================================
            optColumnWidth, query, couponTableData, dataTotal, multipleSelection, 
            // 主表格调用方法 =========================================================================================
            func_handleSearch, handlePageChange, func_changeRowStatus, func_handleAdd, func_handleEdit, func_handleBatchEditStatus, func_handleSelectionChange,
            // 数据执行处理 =========================================================================================
			func_editCouponStatus,
            // 插件业务调用方法 =========================================================================================
			vCouponDialogVisible, vCouponDialogVisible, vCouponData, func_loadvCouponData,
        };
    },
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.green {
    color: #67c23a;
}
.blue {
    color: #1684FC;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>